package com.byx.chatclient.view.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.byx.chatclient.ChatApp;
import com.byx.chatclient.R;
import com.byx.chatclient.adapter.ChooseGroupAdapter;
import com.byx.chatclient.bean.ChooseGroupBean;
import com.byx.chatclient.entity.Result;
import com.byx.chatclient.entity.ResultCallback;
import com.byx.chatclient.entity.ResultCode;
import com.byx.chatclient.factory.RetrofitFactory;
import com.byx.chatclient.http.ActionRequest;
import com.byx.chatclient.utils.GradientStatusBarUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author Superb
 */
public class ChooseGroupActivity extends AppCompatActivity {
    public static final String DATA_KEY = "group";
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.rv_group_list)
    protected RecyclerView mRvGroupList;

    private ChooseGroupAdapter mAdapter;
    private Unbinder mUnbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_group);
        GradientStatusBarUtil.getInstance().bind(this).withBackgroundResource(R.drawable.toolbar).init();
        mUnbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        mAdapter = new ChooseGroupAdapter();
        mAdapter.setOnItemSelectedListener(position -> {
            ChooseGroupBean groupBean = mAdapter.getItem(position);
            Intent intent = new Intent();
            intent.putExtra(DATA_KEY,groupBean);
            setResult(RESULT_OK,intent);
            finish();
        });


        mRvGroupList.setLayoutManager(new LinearLayoutManager(this));
        mRvGroupList.setAdapter(mAdapter);

        Long userId = ChatApp.getInstance().getUserPo().getId();
        RetrofitFactory.getBean(ActionRequest.class).queryUserFriendGroup(userId)
                .enqueue(new ResultCallback<List<ChooseGroupBean>>() {
                    @Override
                    public void onResultSuccess(List<ChooseGroupBean> data) {
                        mAdapter.setList(data);
                    }

                    @Override
                    public void onResultFailure(int code, String msg) {

                    }
                });
    }

    @OnClick({R.id.iv_back,R.id.ll_add_group})
    protected void onClick(View view){
        if (view.getId() == R.id.iv_back){
            finish();
        }else if (view.getId() == R.id.ll_add_group){
            createNewGroup();
        }
    }

    private void createNewGroup() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_add_group_dialog,null);
        Dialog dialog = new AlertDialog.Builder(this).setView(view).create();
        dialog.show();

        view.findViewById(R.id.tv_enter).setOnClickListener(v -> {
            ChooseGroupBean groupBean = new ChooseGroupBean();
            groupBean.setUserId(ChatApp.getInstance().getUserPo().getId());
            groupBean.setGroupName(((EditText) view.findViewById(R.id.et_group_name)).getText().toString());
            RetrofitFactory.getBean(ActionRequest.class).addFriendGroup(groupBean)
                    .enqueue(new ResultCallback<Integer>() {
                        @Override
                        public void onResultSuccess(Integer data) {
                            groupBean.setGroupId(data);
                            mAdapter.addData(groupBean);
                        }

                        @Override
                        public void onResultFailure(int code, String msg) {

                        }
                    });
            dialog.dismiss();
        });
    }

    @Override
    protected void onDestroy() {
        GradientStatusBarUtil.getInstance().unbind();
        mUnbinder.unbind();
        super.onDestroy();
    }
}